{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Save/load street network models to/from disk\n",
    "\n",
    "Author: [Geoff Boeing](https://geoffboeing.com/)\n",
    "\n",
    "  - [Documentation](https://osmnx.readthedocs.io/)\n",
    "  - [Journal article and citation info](https://doi.org/10.1111/gean.70009)\n",
    "  - [Code repository](https://github.com/gboeing/osmnx)\n",
    "  - [Examples gallery](https://github.com/gboeing/osmnx-examples)\n",
    "  \n",
    "This notebook demonstrates how to save networks to disk as GeoPackages, GraphML files, and .osm formatted XML, and how to load an OSMnx-created network from a GraphML file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!uv pip install --system --quiet osmnx[all]\n",
    "from pathlib import Path\n",
    "\n",
    "import osmnx as ox\n",
    "\n",
    "ox.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get a network\n",
    "place = \"Piedmont, California, USA\"\n",
    "G = ox.graph.graph_from_place(place, network_type=\"drive\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GeoPackages for GIS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save graph as a geopackage\n",
    "ox.io.save_graph_geopackage(G, filepath=\"./data/piedmont.gpkg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GraphML files for saving network and preserving topological detail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save/load graph as a graphml file: this is the best way to save your model\n",
    "# for subsequent work later\n",
    "filepath = \"./data/piedmont.graphml\"\n",
    "ox.io.save_graphml(G, filepath)\n",
    "G = ox.io.load_graphml(filepath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# if you want to work with your model in gephi, use gephi compatibility mode\n",
    "ox.io.save_graphml(G, filepath=filepath, gephi=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SVG file to work with in Adobe Illustrator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save street network as SVG\n",
    "fig, ax = ox.plot.plot_graph(G, show=False, save=True, close=True, filepath=\"./images/piedmont.svg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Urban amenities: save points of interest, building footprints, etc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Path(\"data\").mkdir(parents=True, exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get all \"amenities\" and save as a geopackage via geopandas\n",
    "gdf = ox.features.features_from_place(place, tags={\"amenity\": True})\n",
    "gdf = gdf.apply(lambda c: c.astype(str) if c.name != \"geometry\" else c, axis=0)\n",
    "gdf.to_file(\"./data/pois.gpkg\", driver=\"GPKG\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get all building footprints and save as a geopackage via geopandas\n",
    "gdf = ox.features.features_from_place(place, tags={\"building\": True})\n",
    "gdf = gdf.apply(lambda c: c.astype(str) if c.name != \"geometry\" else c, axis=0)\n",
    "gdf.to_file(\"./data/building_footprints.gpkg\", driver=\"GPKG\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save OSM XML files\n",
    "\n",
    "To save your graph to disk as a .osm formatted XML file, ensure that you created the graph with `ox.settings.all_oneway=True` for `save_graph_xml` to work properly. See docstring for details.\n",
    "\n",
    "To save/load full-featured OSMnx graphs to/from disk for later use, use the `save_graphml` and `load_graphml` functions instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save graph to disk as .osm xml file\n",
    "ox.settings.all_oneway = True\n",
    "ox.settings.log_console = True\n",
    "G = ox.graph.graph_from_place(\"Piedmont, California, USA\", network_type=\"drive\", simplify=False)\n",
    "ox.io.save_graph_xml(G, filepath=\"./data/piedmont.osm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
